package com.rtu.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;

import com.rtu.dao.ILzWaterGatherDataDao;
import com.rtu.dao.ILzWaterStationDao;
import com.rtu.dao.IWaterGatherDataDao;
import com.rtu.dao.IWaterStationDao;
import com.rtu.domain.WaterStation;
import com.rtu.dto.QueryStationDto;
import com.rtu.dto.QueryStationRainReportDto;
import com.rtu.dto.RainWaterDto;
import com.rtu.dto.RainWaterReportDto;
import com.rtu.dto.SiphonDto;
import com.rtu.dto.WaterStationResponse;
import com.rtu.service.ILzWaterStationService;
import com.rtu.service.IWaterStationService;

@Service
public class LzWaterStationServiceimpl implements ILzWaterStationService{
	
	@Resource
	private ILzWaterStationDao waterStationDao;
	
	@Resource
	private ILzWaterGatherDataDao waterGatherDataDao;

	@Override
	public Map query(QueryStationDto dto) {
        Map map = new HashedMap();
        List<WaterStationResponse> list = new ArrayList<WaterStationResponse>();
        Integer count = waterStationDao.count();
        if (count == 0) {
            map.put("totalPage",0);
            map.put("totalCount",0);
        } else {
            int totalPage = (count % dto.getPageRowCount() == 0) ? count / dto.getPageRowCount() : count / dto.getPageRowCount() + 1;
            dto.setStart((dto.getPage() - 1) * dto.getPageRowCount());
            list = waterStationDao.query(dto);
            for(WaterStationResponse r : list) {
            	if(StringUtils.isNotBlank(r.getCalculationFormula())) {
            		WaterStation waterStation = new WaterStation();
            		waterStation.setCalculationFormula(r.getCalculationFormula());
            		waterStation.setId(r.getId());
            		
            		String str = waterGatherDataDao.queryData(waterStation);
                	
            		double mptemp =(double)Math.round(Double.parseDouble(str)*1000)/1000;
                	
                	r.setCurrentWaterLevel(String.valueOf(mptemp));
            		
                	//r.setCurrentWaterLevel(waterGatherDataDao.queryData(waterStation));
            	}
            }
            map.put("totalPage",totalPage);
            map.put("totalCount",count);
        }
        map.put("pageRowCount", dto.getPageRowCount());
        map.put("page",dto.getPage());
        map.put("list",list);
        return map;
	}

	@Override
	public Map queryStationById(int id) {
		Map map = new HashedMap();
		WaterStation station = waterStationDao.queryById(id);
		map.put("station", station);
		return map;
	}

	@Override
	public int updateStation(WaterStation station) {
		return waterStationDao.updateStation(station);
	}

	@Override
	public int insertStation(WaterStation station) {
		return waterStationDao.insertStation(station);
	}
	
	@Override
	public int delStationById(int id) {		
		return waterStationDao.delWaterStationById(id);
	}
	
	@Override
	public Map queryWaterlist(QueryStationRainReportDto dto) {
		Map map = new HashedMap();
        List<RainWaterDto> list = new ArrayList<RainWaterDto>(); 
        List<RainWaterDto> mylist = new ArrayList<RainWaterDto>(); 
        Integer count = 0;
        count = waterStationDao.countWaterlist(dto);
    
        if (count == null) {
            map.put("totalPage",0);
            map.put("totalCount",0);

        } else {
           
        	int totalPage = (count % dto.getRows() == 0) ? count / dto.getRows() : count / dto.getRows() + 1;
            dto.setStart(dto.getOffset()  * dto.getRows());
            dto.setPageRowCount(dto.getRows());
            list = waterStationDao.queryWaterlist(dto);
            Double baseVa = 0.0;
            if(dto.getDeviceId() == "18000008") {
            	baseVa=292.029;
            }
            for(RainWaterDto li:list) {
            	
            	String str =li.getVa();
            	Double myva = Double.parseDouble(str);
            	myva= myva+baseVa;
            	String double_str = String.format("%.1f", myva);
            	li.setVa(double_str);
            	//li.setVa(String.valueOf(myva));
            	mylist.add(li);
            	
            }
         	
            
            map.put("totalPage",totalPage);
            map.put("totalCount",count);
          
        }
        map.put("pageRowCount", dto.getPageRowCount());
        map.put("page",dto.getOffset());
       // map.put("list",list);
        map.put("list",mylist);

        return map;
	}
	
	//查询当前水位
	@Override
	public  List<RainWaterDto> queryCurrentWater(QueryStationRainReportDto dto) {
        List<RainWaterDto> list = new ArrayList<RainWaterDto>(); 
		list = waterStationDao.queryCurrentWater(dto);
        if(list.size()==0) {
        	RainWaterDto one = new RainWaterDto();
        	one.setVa("0");
        	String currentHourStr = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
        	one.setCreateTime(currentHourStr);
        	list.add(one);
        }    
        return list;     
	}
	
	@Override
	public List<RainWaterReportDto> queryReportData(QueryStationRainReportDto dto) {

		List<RainWaterReportDto> mylist = new ArrayList<RainWaterReportDto>();
		List<RainWaterReportDto> list = waterStationDao.queryWaterReport(dto);
        
     	for(RainWaterReportDto li:list) {
        	
        	String str =li.getVa();
        	Double myva = Double.parseDouble(str);
        	myva+= 292.029;
        	li.setVa(String.valueOf(myva));
        	mylist.add(li);
        }
     	return mylist;
	}
}
